package com.hnevc.netfoodchoose;

import android.app.DownloadManager;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    EditText mEtUserName;
    EditText mEtUserPassword;
    Button mBtnLogin;
    Button mBtnShowFoods;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        initView();
        initEvent();

    }

    private void initEvent() {
        //TODO 设置按钮的点击事件，完成登陆的验证
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "按钮被点击", Toast.LENGTH_SHORT).show();
            }
        });
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        JSONObject object = new JSONObject();
                        try {
                            object.put("userName", mEtUserName.getText().toString());
                            object.put("userPassword", mEtUserPassword.getText().toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String s = object.toString();
                        Log.e("TAG", "run:" + s);
                        RequestBody body = RequestBody.create(MediaType.parse("application/json"), s);
                        Request request = new Request.Builder()
                                .url("http://10.1.63.232:8080/RestaurantServer/user/doLogin")
                                .post(body)
                                .build();
                        //OkHttpClient client = new OkHttpClient();
                        OkHttpClient client = new OkHttpClient.Builder()
                                .cookieJar(new MyCookieJar())
                                .build();

                        try {
                            Response response = client.newCall(request).execute();
                            //Log.e("TAG", "run:" + response.body().string());
                            String data = response.body().string();
                            if (data!= null && data.length()>0){
                            //正确的用户名
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Intent intent = new Intent();
                                    intent.setClass(MainActivity.this, ShowFoodActivity.class);
                                    startActivity(intent);
                                }
                            });
                        }else{
                            //错误
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MainActivity.this, "用户名密码错误", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                        }catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
        //TODO 设置显示食品列表按钮事件，跳转到SHowFoodActivity完成食品列表的显示

    }

    private void initView() {
        mBtnShowFoods = (Button) findViewById(R.id.id_btn_show_foods);
        mBtnLogin = (Button) findViewById(R.id.id_btn_login);
        mEtUserName = (EditText) findViewById(R.id.id_tv_user_name);
        mEtUserPassword = (EditText) findViewById(R.id.id_et_user_password);
    }
}
